package com.ruoyi.web.controller.business.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.goods.domain.JbCropVariety;
import com.ruoyi.business.goods.service.JbCropVarietyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.business.goods.factory.JbCropVarietyFactory;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 作物 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("作物")
@RestController
@RequestMapping("/crop_variety")
public class JbCropVarietyController extends JoController
{
    @Autowired
    private JbCropVarietyService service;
    @Autowired
    private JbCropVarietyFactory factory;

    /**
     * 查询作物列表
     *
     * @param page          分页查询对象
     * @param jbCropVariety 业务作物对象
     * @return
     */
    @SneakyThrows
    @ApiOperation("查询作物列表")
    // @PreAuthorize("@ss.hasPermi('crop_variety:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbCropVariety jbCropVariety)
    {
        return JoR.rows(factory.page(page, jbCropVariety));
    }

    /**
     * 导出作物列表
     */
    @ApiOperation("导出作物列表")
    @PreAuthorize("@ss.hasPermi('crop_variety:export')")
    @Log(title = "作物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbCropVariety jbCropVariety)
    {
        LambdaQueryWrapper<JbCropVariety> select = new LambdaQueryWrapper<>();
        IPage<JbCropVariety> iPage = service.page(page.b(), select);

        List<JbCropVariety> list = iPage.getRecords();
        ExcelUtil<JbCropVariety> util = new ExcelUtil<JbCropVariety>(JbCropVariety.class);
        util.exportExcel(response, list, "作物数据");
    }

    /**
     * 获取作物详细信息
     */
    @ApiOperation("获取作物详细信息")
    @PreAuthorize("@ss.hasPermi('crop_variety:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增作物
     */
    @ApiOperation("新增作物")
    @PreAuthorize("@ss.hasPermi('crop_variety:add')")
    @Log(title = "作物", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbCropVariety jbCropVariety)
    {
        return toAjax(service.save(jbCropVariety));
    }

    /**
     * 修改作物
     */
    @ApiOperation("修改作物")
    @PreAuthorize("@ss.hasPermi('crop_variety:edit')")
    @Log(title = "作物", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbCropVariety jbCropVariety)
    {
        return toAjax(service.updateById(jbCropVariety));
    }

    /**
     * 删除作物
     */
    @ApiOperation("删除作物")
    @PreAuthorize("@ss.hasPermi('crop_variety:remove')")
    @Log(title = "作物", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("查询作物列表")
//    @PreAuthorize("@ss.hasPermi('crop_variety:listdata')")
    @GetMapping("/listData")
    public AjaxResult listData(JbCropVariety jbCropVariety)
    {
        LambdaQueryWrapper<JbCropVariety> select = new LambdaQueryWrapper<>();
        return AjaxResult.success(service.list(select));
    }
}
